package com.scau.database.finalexam.config;

import com.scau.database.finalexam.DAO.UserDao;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;


public class MyBaits {
    private static SqlSessionFactory sessionFactory;
    public static SqlSessionFactory getSessionFactory( ){
        if(sessionFactory == null) {
            PooledDataSource dataSource = getDataSource();
            TransactionFactory transactionFactory = new JdbcTransactionFactory();
            Environment environment = new Environment("development", transactionFactory, dataSource);
            Configuration configuration = new Configuration(environment);
//            configuration.addMapper(UserDao.class);
            sessionFactory = new SqlSessionFactoryBuilder().build(configuration);
            sessionFactory.getConfiguration().addMapper(UserDao.class);
        }
        return sessionFactory;
    }

    private static PooledDataSource getDataSource(){
        PooledDataSource dataSource = new PooledDataSource();
        dataSource.setDriver("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://106.15.176.164:3306/database");
        dataSource.setUsername("root");
        dataSource.setPassword("lxj971010");
        return dataSource;
    }
}
